/**
 * 认证相关API
 */
import { get, post, patch } from './index'
import type { ApiResponse, LoginRequest, RegisterRequest, LoginResponse, RefreshTokenRequest, User } from '@/types'

// 用户注册
export const register = (data: RegisterRequest) => {
  return post<ApiResponse<User>>('/auth/register/', data)
}

// 用户登录
export const login = (data: LoginRequest) => {
  return post<ApiResponse<LoginResponse>>('/auth/login/', data)
}

// 刷新token
export const refreshToken = (data: RefreshTokenRequest) => {
  return post<ApiResponse<LoginResponse>>('/auth/refresh/', data)
}

// 用户登出
export const logout = () => {
  return post('/auth/logout/')
}

// 获取当前用户信息
export const getCurrentUser = () => {
  return get<ApiResponse<User>>('/auth/user/')
}

// 获取用户信息（别名）
export const getUserInfo = getCurrentUser

// 更新用户信息
export const updateProfile = (data: Partial<User>) => {
  return patch<ApiResponse<User>>('/auth/profile/', data)
}

// 修改密码
export const changePassword = (data: { old_password: string; new_password: string }) => {
  return post('/auth/change-password/', data)
}

// 重置密码
export const resetPassword = (data: { email: string }) => {
  return post('/auth/reset-password/', data)
}

// 验证token有效性
export const verifyToken = () => {
  return get<ApiResponse<{ valid: boolean }>>('/auth/verify-token/')
}

// 导出统一的认证API对象
export const authApi = {
  // 用户认证
  register,
  login,
  refreshToken,
  logout,

  // 用户信息
  getCurrentUser,
  getUserInfo,
  updateProfile,

  // 密码管理
  changePassword,
  resetPassword,

  // 工具函数
  verifyToken
}